package com.itheima.tanhua.server.service;

import com.itheima.tanhua.domain.UserInfo;
import com.itheima.tanhua.dubbo.api.UserApi;
import com.itheima.tanhua.po.RecommendationPo;
import com.itheima.tanhua.server.holder.TokenHolder;
import com.itheima.tanhua.vo.TodayBestVo;
import lombok.Data;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author itheima
 * @since 2022-01-14
 */
@Service
public class TanhuaService {

    @DubboReference
    private UserApi userApi;

    public TodayBestVo getTodayBest() {
        String id = TokenHolder.get();

        UserInfo myUserInfo = userApi.findUserInfoByUserId(id);
        if (Objects.isNull(myUserInfo)) {
            return new TodayBestVo("1",
                    "https://hbimg.huabanimg.com/3ec69aeea6ebd7f039caf83cf8fb8926bab3df71a3ee1-z195dg_fw658/format/webp",
                    "随机",
                    "女",
                    18,
                    null,
                    99);
        }

        String myCity = myUserInfo.getCity();
        UserInfo todayBestUserInfo = userApi.randomUserByCity(id, myCity);

        String tags = todayBestUserInfo.getTags();
        List<String> tagList = new ArrayList<>();
        if (StringUtils.isNoneEmpty(tags)) {
            tagList = Arrays.asList(tags.split(","));
        }

        TodayBestVo vo = new TodayBestVo();
        BeanUtils.copyProperties(todayBestUserInfo, vo);
        vo.setTags(tagList);
        vo.setFateValue(RandomUtils.nextInt(90, 99));

        return vo;
    }

    public List<UserInfo> getRecommendationList(RecommendationPo recommendationPo) {
        String id = TokenHolder.get();
        return userApi.findRecommendUserList(id, recommendationPo);
    }
}
